﻿
// 根据生成规则，判断最大值的长度是否超过用户设置的流水号长度
function serialNumber_CheckSetting() {
    var validate = $("form").form("validate");
    if (!validate) {
        return false;
    }

    var result = true;
    var startNumber = Number(viewModel.form.StartNumber());
    var totalNumber = Number(viewModel.form.TotalNumber());
    var stepNumber = Number(viewModel.form.StepNumber());
    var numberLength = Number(viewModel.form.NumberLength());

    var prefix = viewModel.form.Prefix();
    var postfix = viewModel.form.Postfix();
    var sample = prefix + startNumber.zeroize(numberLength) + postfix;
    sample = '根据该设置生成的示例序列号为：' + sample + '<br/>';

    var note = '';
    var maxNumber = startNumber + (totalNumber - 1) * stepNumber;
    var maxNumberLength = maxNumber.toString().length;
    if (maxNumberLength > numberLength) {
        note = '根据该设置生成的最大流水号为：' + maxNumber.toString() + '，其长度已超过【流水号长度】，请修正';
        note = '<span style="color:red">' + note + '</span>';
        result = false;
    }

    $("#txtRemark").html(sample + note);
    return result;
}

var VMSerialNumber = function () {
    var me = this;

    function initMeta() {

        // 从Cookie读取设置
        var cookieName = 'Cookie_SerialNumber_';
        var prefix = commonCookieHelper.get(cookieName + 'Prefix');
        var postfix = commonCookieHelper.get(cookieName + 'Postfix');
        var startNumber = commonCookieHelper.get(cookieName + 'StartNumber');
        var totalNumber = commonCookieHelper.get(cookieName + 'TotalNumber');
        var stepNumber = commonCookieHelper.get(cookieName + 'StepNumber');
        var numberLength = commonCookieHelper.get(cookieName + 'NumberLength');
        if (prefix == null || prefix == undefined || prefix.length <= 0) {
            // 系统设置的前缀：D+日期+T+时间+S
            prefix = 'D' + (new Date()).format('yyyyMMddThhmmss') + 'S';
        }
        if (postfix == null) {
            postfix = '';
        }
        if (startNumber == null) {
            startNumber = 1;
        }
        if (totalNumber == null) {
            totalNumber = 3;
        }
        if (stepNumber == null) {
            stepNumber = 1;
        }
        if (numberLength == null) {
            numberLength = 3;
        }

        var result = {
            "FormData": {
                "ItemData": { "Prefix": prefix, "StartNumber": startNumber, "Postfix": postfix, "TotalNumber": totalNumber, "StepNumber": stepNumber, "NumberLength": numberLength },
                "FormID": 1, "FormName": "SerialNumber", "RecordID": -1, "CurrentTaskID": -1, "NewFlowStatusID": -1, "ChildData": {}
            },
            "ItemMetadata": {
                "Prefix": { "DisplayName": "前缀", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeSinglelineText", "EnableEdit": true, "IsRequired": false },
                "StartNumber": { "DisplayName": "起始流水号", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeInteger", "EnableEdit": true, "IsRequired": true },
                "Postfix": { "DisplayName": "后缀", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeSinglelineText", "EnableEdit": true, "IsRequired": false },
                "TotalNumber": { "DisplayName": "生成数量", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeInteger", "EnableEdit": true, "IsRequired": true },
                "StepNumber": { "DisplayName": "步长", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeInteger", "EnableEdit": true, "IsRequired": true },
                "NumberLength": { "DisplayName": "流水号长度", "DisplayWidth": 100, "EnableView": true, "DataTypeInfo": "TypeInteger", "EnableEdit": true, "IsRequired": true },
                "ChildMetadata": {}
            }
        };

        return result;
    }
    this.data = initMeta();

    this.form = ko.mapping.fromJS(me.data.FormData.ItemData);

    this.submitClick = function () {
        var validate = $("form").form("validate");
        if (!validate || !serialNumber_CheckSetting()) {
            return false;
        }

        if (!me.data.FormData) {
            $.messager.alert('错误', "参数错误！", 'error');
            return false;
        }

        var prefixOrigin = me.form.Prefix();
        var postfixOrigin = me.form.Postfix();
        var prefix = '';
        var postfix = '';
        if (prefixOrigin != null && prefixOrigin != undefined) {
            prefix = prefixOrigin.toString();
        }
        if (postfixOrigin != null && postfixOrigin != undefined) {
            postfix = postfixOrigin.toString();
        }

        var startNumber = Number(me.form.StartNumber());
        var totalNumber = Number(me.form.TotalNumber());
        var stepNumber = Number(me.form.StepNumber());
        var numberLength = Number(me.form.NumberLength());

        var serialNumbers = new Array();
        var currentNumber = startNumber;
        for (var i = 0; i < totalNumber; i++) {
            var serial = prefix + currentNumber.zeroize(numberLength) + postfix;
            serialNumbers.push(serial);

            currentNumber += stepNumber;
        }

        // 保存设置到Cookie
        var cookieName = 'Cookie_SerialNumber_';
        if (prefix.substr(0, 9) != 'D' + (new Date()).format('yyyyMMdd')) {
            // 系统自动设置的前缀不保存，只保存用户设置的前缀
            commonCookieHelper.set(cookieName + 'Prefix', prefix);
        }
        commonCookieHelper.set(cookieName + 'Postfix', postfix);
        commonCookieHelper.set(cookieName + 'StartNumber', startNumber);
        commonCookieHelper.set(cookieName + 'TotalNumber', totalNumber);
        commonCookieHelper.set(cookieName + 'StepNumber', stepNumber);
        commonCookieHelper.set(cookieName + 'NumberLength', numberLength);

        if (top.getLastLayer) {
            var lastLayer = top.getLastLayer();
            if (lastLayer && lastLayer.updateSerialNumberValue) {
                lastLayer.updateSerialNumberValue(serialNumbers);
            }
        }
    };

    this.cancelClick = function () {
        top.closeDialog();
    };
};

$(function () {
    var prefix = "#formSerialNumber_";

    $(prefix + "Prefix")[0].exEventChange = serialNumber_CheckSetting;
    $(prefix + "Postfix")[0].exEventChange = serialNumber_CheckSetting;
    $(prefix + "StartNumber")[0].exEventChange = serialNumber_CheckSetting;
    $(prefix + "TotalNumber")[0].exEventChange = serialNumber_CheckSetting;
    $(prefix + "StepNumber")[0].exEventChange = serialNumber_CheckSetting;
    $(prefix + "NumberLength")[0].exEventChange = serialNumber_CheckSetting;
});